<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
<script src="../vue.js"></script>
</head>

<body>
<div id="app">

 <h1>{{person.name}}--{{person.age}}</h1>
 <button @click="update">改变年龄</button>
 <hr>
 <button @click="isHot=!isHot">改变天气</button>
 <h1>今天的天气真{{isHot?"热":"冷"}}</h1>
 <img :src="url" alt="">
 </div>
 <script>
    Vue.config.productionTip=false;
     
     const vm =new Vue({
        el:"#app",
        data() {
            return {
                isHot:true,
                url:"",
                person:{
                    name:"laowang",
                    age:19
                }
            }
        },
        watch:{
            isHot:{
                //watch默认只能监视某个属性的变化,并不能在初始化的时候就执行handler函数
                //可以配置immediate属性,让watch在初始化阶段就立即监视
                immediate:true,
                handler(newValue,oldValue) {
                    console.log("handle",newValue,oldValue);
                    this.url=newValue?"./re.webp":"./leng.gif"
                }
            },
            person:{
                //对一个对象或者数字监视的时候,内部发送改变并不能触发handle 需要开启深度监视
                deep:true,
                handler(newPer,oldPer) {
                    console.log(newPer.age,oldPer.age);
                    alert("你老了")
                }
            }
        },
        methods:{
            update(){
                this.$set(this.person,"age",this.person.age+1)
            }
        }
     })
 </script>
 </script>
</body>

</html>